﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using AutoPartsSearch.Domain.Abstract.Repositories;
using AutoPartsSearch.Domain.Entities;
using AutoPartsSearch.Domain.Common;
using AutoPartsSearch.Domain.Abstract;

namespace AutoPartsSearch.Domain.Concrete.Repositories
{
    public class DapperSellerSearchQueryRepository : BaseRepository, ISellerSearchQueryRepository
    {
        public DapperSellerSearchQueryRepository(ILanguageGetter languageGetter) : base(languageGetter)
        {
        }

        public int Add(SellerSearchQuery instance)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@SellerId", instance.SellerId, DbType.Int32);
            param.Add("@SearchQueryId", instance.SearchQuery.Id, DbType.Int32);
            return QueryExecutor.ExecuteProcedure("sp_AddSellerSearchQuery", param);
        }

        public void Update(SellerSearchQuery instance)
        {
            throw new NotImplementedException();
        }

        public SellerSearchQuery GetById(int id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<SellerSearchQuery> Get()
        {
            throw new NotImplementedException();
        }

        public IEnumerable<SellerSearchQuery> Get(int page, int pageSize)
        {
            throw new NotImplementedException();
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        public long GetTotalCount()
        {
            throw new NotImplementedException();
        }

        public void Process(SellerSearchQuery sellerSearchQuery)
        {
            throw new NotImplementedException();
        }

        public void Delete(int queryId, int sellerId)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@SellerId", sellerId, DbType.Int32);
            param.Add("@QueryId", queryId, DbType.Int32);
            QueryExecutor.ExecuteProcedure("sp_DeleteSellerSearchQuery", param);
        }

        public void MarkAsProcessed(int queryId, int sellerId)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@SellerId", sellerId, DbType.Int32);
            param.Add("@SearchQueryId", queryId, DbType.Int32);
            param.Add("@IsProcessed", true, DbType.Boolean);
            QueryExecutor.ExecuteProcedure("sp_UpdateSellerSearchQuery", param);
        }

        public IEnumerable<SellerSearchQuery> GetBySeller(int sellerId, int page, int pageSize, out int totalCount, bool? processed = null)
        {
            var total = 0;
            var param = new Dapper.DynamicParameters();
            param.Add("@SellerId", sellerId, DbType.Int32);
            param.Add("@Page", page, DbType.Int32);
            param.Add("@PageSize", pageSize, DbType.Int32);
            param.Add("@Processed", processed, DbType.Boolean);
            param.Add("@Lang", this.CurrentLanguage, DbType.String);
            param.Add("@TotalCount", dbType: DbType.Int32, direction: ParameterDirection.Output);
            var result = QueryExecutor.QueryDynamic<SellerSearchQuery>("sp_GetSellerSearchQueriesBySeller", ModelReader, param).ToList();
            totalCount = param.Get<int>("@TotalCount");
            return result;
        }

        public SellerSearchQuery GetBySearchQueryAndSeller(int queryId, int sellerId)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@QueryId", queryId, DbType.Int32);
            param.Add("@SellerId", sellerId, DbType.Int32);
            param.Add("@Lang", this.CurrentLanguage, DbType.String);
            return QueryExecutor.QueryDynamic<SellerSearchQuery>("sp_GetSellerSearchQueriesByQueryAndBySeller", ModelReader, param)
                .FirstOrDefault();
        }

        public int GetTotalCount(int sellerId, bool? processed = null)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@SellerId", sellerId, DbType.Int32);
            param.Add("@Processed", processed, DbType.Boolean);
            return QueryExecutor.QueryProcedure<int>("sp_GetSellerSearchQueriesTotalCountBySeller", param).FirstOrDefault();
        }

        public bool HasNewRequests(string userName)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@UserName", userName, DbType.String);
            param.Add("@Date", DateTime.Now.AddMinutes(-1), DbType.DateTime);
            var res = QueryExecutor.QueryProcedure<int>("sp_HasSellerSearchQueriesMoreThanDate", param).FirstOrDefault();
            return res == 1 ? true : false;
        }

        public DateTime GetSellerEpiredDate(int sellerId)
        {
            throw new NotImplementedException();
        }

        private void ModelReader(SellerSearchQuery sellerSearchQuery, dynamic o)
        {
            sellerSearchQuery.SellerId = o.SellerId;
            sellerSearchQuery.IsProcessed = o.IsProcessed;

            sellerSearchQuery.SearchQuery = new SearchQuery
                                                {
                                                    Id = o.SearchQueryId,
                                                    PartName = o.PartName,
                                                    PartGroup = new Dictionary(o.PartGroup),
                                                    PartType = new Dictionary(o.PartType),
                                                    PartCatalogNumber = o.PartCatalogNumber,
                                                    PartState = new Dictionary(o.PartState),
                                                    PartDescription = o.PartDescription,
                                                    CarType = new Dictionary(o.CarType),
                                                    CarYear = o.CarYear,
                                                    CarBrand = new Dictionary(o.CarBrand),
                                                    CarModel = o.CarModel,
                                                    CarModification = o.CarModification,
                                                    CarVin = o.CarVin,
                                                    CarEngine = o.CarEngine,
                                                    CarCase = new Dictionary(o.CarCase),
                                                    CarTransmission = new Dictionary(o.CarTransmission),
                                                    CarDrive = new Dictionary(o.CarDrive),
                                                    CarFuelType = new Dictionary(o.CarFuelType),
                                                    City = new Dictionary(o.City),
                                                    Name = o.PartName,
                                                    Phone = o.Phone,
                                                    Phone2 = o.Phone2,
                                                    Email = o.Email,
                                                    Comment = o.Comment,
                                                    Created = o.Created,
                                                };
        }
    }
}
